﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using AcctrueWMS.Foundation.DAL;
using System.Data;
namespace AcctrueWMS.Web
{
    /// <summary>
    /// 对项目报警进行处理
    /// </summary>
    public partial class dopost_alerm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["alerm"] != null)
            { //进行报警
                GetAlermData();

            }
        }

        ///// <summary>
        ///// 返回给 客户端需要报警的项目ID集合
        ///// </summary>
        //private void GetNeedAlermProjectL() {
        //    string[] sign={"(%)"};//数据分割标志
        //    string[] pIDL = Request.Form["IDL"].Split(sign,StringSplitOptions.RemoveEmptyEntries);
        //   StringBuilder sb=new StringBuilder();
        //   StringBuilder sb2 = new StringBuilder();
        //    for (int i = 0; i <pIDL.Length; i++)
        //    {
        //        sb.Append("'");
        //        sb.Append(pIDL[i]);
        //        sb.Append("'");
        //        if(i<pIDL.Length-1){
        //        sb.Append(",");
        //        }
        //    }
        //    if (sb.Length > 0)
        //    {
        //        string sql = "select projectGUID from dbo.projectdismemberment where realTime>expectedTime and projectGUID in (" + sb.ToString() + ") group by projectGUID";
        //        DataTable dt = WMSqlExcuter.ExecuteDataTable(sql, CommandType.Text);

        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            sb2.Append(dt.Rows[i].ItemArray[0].ToString());
        //            if (i < dt.Rows.Count - 1)
        //            {

        //                sb2.Append("(*)");
        //            }
        //        }
        //    }
        //    Response.Write(sb2.ToString());
        //    Response.End();
        //}


        /// <summary>
        /// （报警1） 项目报警，该报警是根据项目中的项目分解项 完成时间没有在期望时间内完成
        /// </summary>
        private string GetNeedAlermProjectByDismemberment()
        {
            string[] sign = { "(%)" };//数据分割标志
            string[] pIDL = Request.Form["IDL"].Split(sign, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder sb = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < pIDL.Length; i++)
            {
                sb.Append("'");
                sb.Append(pIDL[i]);
                sb.Append("'");
                if (i < pIDL.Length - 1)
                {
                    sb.Append(",");
                }
            }
            if (sb.Length > 0)
            {
                string sql = "select projectGUID from dbo.projectdismemberment where realTime>expectedTime and projectGUID in (" + sb.ToString() + ") group by projectGUID";
                DataTable dt = WMSqlExcuter.ExecuteDataTable(sql, CommandType.Text);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb2.Append(dt.Rows[i].ItemArray[0].ToString());
                    if (i < dt.Rows.Count - 1)
                    {

                        sb2.Append("(*)");
                    }
                }
            }
            return sb2.ToString();
        }

       /// <summary>
        /// （报警2）项目报警，该报警是根据项目报告中的问题在期望时间内没有解决，则该项目报告所对应的项目就需要进行报警
       /// </summary>
       /// <returns></returns>
        private string  GetNeedAlermProjectLByQuestion() {
            string[] sign = { "(%)" };//数据分割标志
            string[] pIDL = Request.Form["IDL"].Split(sign, StringSplitOptions.RemoveEmptyEntries);
            StringBuilder sb = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < pIDL.Length; i++)
            {
                sb.Append("'");
                sb.Append(pIDL[i]);
                sb.Append("'");
                if (i < pIDL.Length - 1)
                {
                    sb.Append(",");
                }
            }
            if (sb.Length > 0)
            {
                string sql = "select projectQuestion.projectGUID from projectSolution inner join projectQuestion on(projectSolution.repotGuid=projectQuestion.repotGuid) where projectSolution.repotGuid in(select repotGuid from projectQuestion where projectGUID in ("+sb.ToString()+")) and expectSolveTime<dateadd(d,-1,dateadd(d,1,convert(datetime,Convert(varchar(10),getDate(),120),120)))  and ((questionState<>'05' and questionState<>'04') or questionState is null) group by projectQuestion.projectGUID";//当问题的状态不是 已关闭 或者已解决时 并且在 期望时间内没有解决  进行报警
                DataTable dt = WMSqlExcuter.ExecuteDataTable(sql, CommandType.Text);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    sb2.Append(dt.Rows[i].ItemArray[0].ToString());
                    if (i < dt.Rows.Count - 1)
                    {

                        sb2.Append("(@)");
                    }
                }
            }
            return sb2.ToString();


        }

        /// <summary>
        /// 获取所有的报警数据
        /// </summary>
        private void GetAlermData() {
            //(#)各种报警类型数据间的分隔符
            Response.Write(GetNeedAlermProjectByDismemberment() + "(#)" + GetNeedAlermProjectLByQuestion());
            Response.End();
        
        }
    }
}